<?php
namespace Home\Model;
use Think\Model;

class UserInfoModel extends Model {
    protected $_validate = array(
        array('mobile', 'require', '手机号不能为空', 0, 'regex', 3),
        array('mobile', '/^1[3|4|7|5|8]\d{9}$/', '手机号格式不正确', 0, 'regex', 3),
        array('mobile', 'checkMobileExist', '手机号已被注册', 0, 'callback', 3),
        array('email', '/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i', '邮箱格式不正确', 2, 'regex', 3),
        array('email', 'checkEmailExist', '邮箱已被注册', 2, 'callback', 3),
    );

    protected $_map = array(
        'edit_status' =>'status', // 把表单中name映射到数据表的username字段

    );

    protected $_auto=array(
        array('realnamepy', 'z2p', 3, 'callback'),
        array('workunit', '武汉广电', 3, 'string'),
    );


    /**
     * 检测邮箱是否被注册
     * @return [type] [description]
     */
    protected function checkEmailExist() {
        $email = I('post.email');
        $map['email'] = $email;
        if(I('post.uid')){
            $map['uid']=array('neq',I('post.uid'));
        }

        $res = M('UserInfo')->where($map)->getField('uid');
        if($res)
            return false;
        else
            return true;
    }

    /**
     * 检测手机是否被注册
     * @return [type] [description]
     */
    protected function checkMobileExist() {
        $mobile = I('post.param');
        $map['mobile'] = $mobile;
        if(I('post.uid')){
            $map['uid']=array('neq',I('post.uid'));
        }

        $res = M('UserInfo')->where($map)->getField('uid');
        if($res)
            return false;
        else
            return true;
    }

    protected function z2p(){
        return zh2py(I('post.realname'));
    }
}
